package com.Iconcat.system.controller;

import com.Iconcat.system.entity.CatNotise;
import com.Iconcat.system.entity.SystemSetting;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.CatNotiseService;
import com.Iconcat.system.service.SystemSettingsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/CatNotise")
@CrossOrigin
@Api(value = "系统公告模块",tags = "系统公告接口")
public class CatNotiseController {
    @Resource
    private CatNotiseService catNotiseService;

    @GetMapping("/notise/all")
    public Result All(@RequestParam(required = true,defaultValue = "1") Integer current,
                      @RequestParam(required = true,defaultValue = "6")Integer size

    ){
        Page<CatNotise> page=new Page<>(current,size);
        LambdaQueryWrapper<CatNotise> queryWrapper=new LambdaQueryWrapper<>();
        Page<CatNotise> userPage=catNotiseService.page(page,queryWrapper);
        return Result.ok().data("Notise",userPage);
    }
    @GetMapping("/look/{id}")
    public Result getSystemSetting(@PathVariable Integer id) {
        CatNotise notise = catNotiseService.getById(id);
        if (notise != null) {
            return Result.ok().data("notise", notise);
        } else {
            return Result.error().message("notise not found");
        }
    }

    @PostMapping("/look/add")
    public Result createSystemSetting(@RequestBody CatNotise catNotise) {
        boolean success = catNotiseService.save(catNotise);
        if (success) {
            return Result.ok().message("catNotise created successfully");
        } else {
            return Result.error().message("Failed to create catNotise");
        }
    }

    @PostMapping("/look/update/{id}")
    public Result updateSystemSetting(@PathVariable Integer id, @RequestBody CatNotise catNotise) {
        catNotise.setNoId(id);
        boolean success = catNotiseService.updateById(catNotise);
        if (success) {
            return Result.ok().message("System setting updated successfully");
        } else {
            return Result.error().message("Failed to update system setting");
        }
    }

    @PostMapping("/look/delete/{id}")
    public Result deleteSystemSetting(@PathVariable Integer id) {
        boolean success = catNotiseService.removeById(id);
        if (success) {
            return Result.ok().message("System setting deleted successfully");
        } else {
            return Result.error().message("Failed to delete system setting");
        }
    }
}
